﻿using ZYCommon;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Xml;
using Paint;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    public class ZYTextImage : ZYTextObject, IDisposable
    {
        private Image myImage = null;
        private Image editorImage = null;

        public ZYTextImage()
        {

            base.myBorder = new ZYTextBorder();
            base.intWidth = 30;
            base.intHeight = 30;
        }

        public void Dispose()
        {
            if (this.myImage != null)
            {
                this.myImage.Dispose();
                this.myImage = null;
            }
            if (this.editorImage != null)
            {
                this.editorImage.Dispose();
                this.editorImage = null;
            }
        }

        public bool EditImage()
        {
            Image newimage = new Bitmap(this.Image);
            using (frmMain imgMain = new frmMain(newimage, this.Width, this.Height, this.GetEditorString))
            {
                imgMain.MyDocument.editorImg = GetEditorImg;
                imgMain.ShowDialog();
            }
            return true;
        }

        void GetEditorImg(Image _img, string loadString)
        {
            if (!string.IsNullOrEmpty(loadString))
            {
                this.EditorImage = _img;
                this.GetEditorString = loadString;
            }
        }

        /// <summary>
        /// 修改图片
        /// </summary>
        /// <param name="img"></param>
        public void UpdateImage(Bitmap img)
        {
            this.Image = (Image)img;
            this.myOwnerDocument.RefreshSize();
        }

        public override bool FromXML(XmlElement myElement)
        {
            if (base.FromXML(myElement))
            {
                this.Width = 30;
                this.Height = 30;
                if (this.myImage != null)
                {
                    this.myImage.Dispose();
                    this.myImage = null;
                }
                if (this.SaveInFile)
                {
                    this.myImage = StringCommon.ImageFromBase64String(myElement.InnerText);
                }
                else
                {
                    this.myImage = ZYTextConst.ImageFromURL(this.Src);
                }
                if (this.myImage != null)
                {
                    this.Width = this.myImage.Size.Width;
                    this.Height = this.myImage.Size.Height;
                }
                if (myElement.HasAttribute("width"))
                {
                    this.Width = StringCommon.ToInt32Value(myElement.GetAttribute("width"), base.intWidth);
                }
                if (myElement.HasAttribute("height"))
                {
                    this.Height = StringCommon.ToInt32Value(myElement.GetAttribute("height"), base.intHeight);
                }
                base.myAttributes.SetValue("width", this.intWidth.ToString());
                base.myAttributes.SetValue("height", this.intHeight.ToString());
                return true;
            }
            return false;
        }

        public override string GetXMLName()
        {
            return "img";
        }

        //public override bool HandleDblClick(int x, int y, MouseButtons Button)
        //{
        //    if ((this.myImage != null) && this.myBounds.Contains(x, y))
        //    {
        //        this.EditImage();
        //        return true;
        //    }
        //    return false;
        //}

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            if (base.HandleMouseMove(x, y, Button))
            {
                return true;
            }
            if ((this.myImage != null) && this.myBounds.Contains(x, y))
            {
                base.myOwnerDocument.SetToolTip(string.Concat(new object[] { "图片宽度:", this.myImage.Size.Width, " 高度:", this.myImage.Size.Height, "\r\n格式:", this.myImage.PixelFormat.ToString() }), -1, new Rectangle(x, y, 5, 5));
                return true;
            }
            return false;
        }

        public override bool isTextElement()
        {
            return false;
        }

        public bool LoadImage()
        {
            string str;
            if (((str = this.ImageType) != null) && (string.IsInterned(str) == "emrtextdoc"))
            {
                using (ZYTextDocument document = new ZYTextDocument())
                {
                    document.View = new DocumentView();
                    document.FromXMLFile(this.Src);
                    this.myImage = document.GetPreViewImage();
                }
            }
            else
            {
                this.myImage = ZYTextConst.ImageFromURL(this.Src);
            }
            if (this.myImage != null)
            {
                base.intWidth = this.myImage.Size.Width;
                base.intHeight = this.myImage.Size.Height;
            }
            return true;
        }

        public override bool RefreshView()
        {
            int realLeft = this.RealLeft;
            int realTop = this.RealTop;
            if (this.myImage != null)
            {
                if (this.EditorImage != null)
                {
                    base.myOwnerDocument.View.DrawImage(this.editorImage, this.Bounds.X, this.Bounds.Y, this.Bounds.Width, this.Bounds.Height);
                }
                else
                {
                    base.myOwnerDocument.View.DrawImage(this.myImage, this.Bounds.X, this.Bounds.Y, this.Bounds.Width, this.Bounds.Height);
                }
            }
            else
            {
                base.myOwnerDocument.View.DrawString("未找到图片文件", realLeft, realTop, base.intWidth, base.intHeight);
            }
            return base.RefreshView();
        }

        public override bool ToXML(XmlElement myElement)
        {
            switch (base.myOwnerDocument.Info.SaveMode)
            {
                case 0:
                    if (this.myImage == null)
                    {
                        goto Label_00C3;
                    }
                    if (this.myImage.Size.Width == base.intWidth)
                    {
                        base.myAttributes.RemoveAttribute("width");
                        break;
                    }
                    base.myAttributes.SetValue("width", this.intWidth.ToString());
                    break;

                default:
                    return false;
            }
            if (this.myImage.Size.Height != base.intHeight)
            {
                base.myAttributes.SetValue("height", this.intHeight.ToString());
            }
            else
            {
                base.myAttributes.RemoveAttribute("height");
            }
        Label_00C3:
            base.myAttributes.ToXML(myElement);
            if (this.SaveInFile && (this.myImage != null))
            {
                myElement.InnerText = StringCommon.ImageToBase64String(this.myImage, ImageFormat.Png);
            }
            return true;
        }

        public System.Drawing.Image Image
        {
            get
            {
                return this.myImage;
            }
            set
            {
                this.myImage = value;

                if (this.myImage != null)
                {
                    this.Width = this.myImage.Size.Width;
                    this.Height = this.myImage.Size.Height;
                }
            }
        }

        public string GetEditorString
        {
            get
            {
                return base.myAttributes.GetString("shapesStr");
            }
            set
            {
                base.myAttributes.SetValue("shapesStr", value);
            }
        }

        public Image EditorImage
        {
            get
            {
                if (this.editorImage == null)
                {
                    string text = this.myAttributes.GetString("newimgStr");
                    if (!string.IsNullOrEmpty(text))
                    {
                        this.editorImage = StringCommon.ImageFromBase64String(text);
                    }
                }
                return this.editorImage;
            }
            set
            {
                this.myAttributes.SetValue("newimgStr", StringCommon.ImageToBase64String(value, ImageFormat.Png));
                this.editorImage = value;
            }
        }

        public bool ImageSizeRectLock
        {
            get
            {
                return (Math.Abs((double)(base.dblWidthHeightRate - (((double)this.myImage.Width) / ((double)this.myImage.Height)))) < 0.04);
            }
            set
            {
                if (value && (this.myImage != null))
                {
                    base.dblWidthHeightRate = ((double)this.myImage.Width) / ((double)this.myImage.Height);
                }
                else
                {
                    base.dblWidthHeightRate = 0.0;
                }
            }
        }

        public string ImageType
        {
            get
            {
                return base.myAttributes.GetString("type");
            }
            set
            {
                base.myAttributes.SetValue("type", value);
            }
        }

        public bool SaveInFile
        {
            get
            {
                return base.myAttributes.GetBool("saveinfile");
            }
            set
            {
                base.myAttributes.SetValue("saveinfile", value);
            }
        }

        public string Src
        {
            get
            {
                return base.myAttributes.GetString("src");
            }
            set
            {
                base.myAttributes.SetValue("src", value);
            }
        }
    }
}

